/**
 * 消毒明细查询用js
 *    ※
 * 
 * */
var MY_INSTRUMENT_DISINFECT_COMMON = (function() {
	
	/**
	 * 查看明细
	 * 
	 * @param 查询参数
	 * @param $tHead_SpecimenDetailList  table_head jqeruy对象
	 * @param $tbodySpecimenDetailList   table_body jqeruy对象
	 * 
	 * */
	function loadDetailItem( params ,$tHead_SpecimenDetailList,$tbodySpecimenDetailList){
		
		// 标本列表
		$tbodySpecimenDetailList.empty();
		$tHead_SpecimenDetailList.empty();
		// 器械、场所 table头 
		var headHtml = getTblHead( params.businessSubType );
		$tHead_SpecimenDetailList.append(headHtml);
		
		WeKnow.loading();
		
		$.ajax({ 
	        type: "POST", 
	        url: "invs_instrument_disinfect.do/load_detail", 
	        dataType: "json",
	        contentType:"application/json",
	        data: JSON.stringify(params),
	        async: true,//异步调用
	        success: function(data) {
	        	
	        	WeKnow.closeLoading();
	        	
				if (!WeKnow.filterAjaxResult(data)) return false; // session过期check
	        	
	        	if (data.errcode == 0) {
	        		// 把取到的结果放到
	        		var e = '';
	        		
	        		if ( 'datalist' in data && data.datalist.length == 0 ){
	        			WeKnow.errorMsg('没有明细数据');
	        			return;
	        		}
	        		$(data.datalist).each(function( idx,_a){
	        			
	        			// 已消毒的数据做处理
        				if ( _a.checkSt == 0 ){
        					
        					var curRowspanStr = 'rowspan='+ _a.headEntity.detailList.length;
        					
        					// 正常登记的数据
        					var detalList = _a.headEntity.detailList;
        					if (detalList.length){
        						$(detalList).each(function( idx1,_b){
            						e+='<tr';
            						    e+= ' data-id="' + _a.id + '"';
    	        						e+= ' data-institution_id="' + _a.headEntity.institutionId + '"';
    			        				e+= ' data-paper_img_id="' + _a.headEntity.paperImgId + '"';
    			        				e+= ' data-serial_date="' + _a.serialDate + '"';
    			        				if (  _a.appeal && _a.appeal.appealAtta ){ //申诉图片
    	        							e+= ' data-appeal_atta="' + _a.appeal.appealAtta + '"';
    	        						}
    		        				e+= '>';
    		        				
            						if (idx1 == 0 ){
										// 名称
										e+='<td class="text-center" '+ curRowspanStr +' >'+ _a.institutionShortName + '</td>';
            							// 日期
        		        				e+='<td class="text-center" '+ curRowspanStr +' >'+ _a.serialDate + '</td>';
        		        				// 状态
        		        				e+='<td class="text-center " '+ curRowspanStr +' >正常</td>';
        		        				// 状态说明
        		        				e+='<td class="text-center message" '+ curRowspanStr +' >'+(_a.message !=null && _a.msgId!= null?_a.message:'-')+'</td>';
        		        				// 器械名称
        		        				e+='<td class="text-center">'+ (_b.instrumentName == null?'-':_b.instrumentName)  + '</td>';
        		        				if ( params.businessSubType == '101' ){ //器械消毒时
        		        					// 数量
    	    		        				e+='<td class="text-center">'+ (_b.quantity == null?'-':_b.quantity)  + '</td>';
        		        				}
        		        				// 消毒方法
        		        				e+='<td class="text-center" '+ curRowspanStr +' >'+ (_a.disinfectTypeTxt == null?'-':_a.disinfectTypeTxt)  + '</td>';
        		        				// 消毒时间
                                        // if (params.businessSubType === '102') {
											// e+='<td class="text-center" '+ curRowspanStr +' >'+ (_a.headEntity.disinfectStartTm == null?'': new Date(_a.headEntity.disinfectStartTm).format('hh:mm')) + ' ~ '
											// 	+ (_a.headEntity.disinfectEndTm == null?'': new Date(_a.headEntity.disinfectEndTm).format('hh:mm')) + '</td>';
                                        // }

										// e+='<td class="text-center" '+ curRowspanStr +' >'+ (_a.headEntity.disinfectStartTm == null?'': new Date(_a.headEntity.disinfectStartTm).format('hh:mm'))  + '</td>';
										// 消毒员
        		        				e+='<td class="text-center" '+ curRowspanStr +' >'+ (_a.doctorName == null?'-':_a.doctorName)  + '</td>';
        		        				if ( params.businessSubType == '101'){ //器械消毒时

        		        					if ( _a.headEntity.paperImgId != null ){
												e+='<td class="text-center" '+ curRowspanStr +' ><button type="button" name="showImage" class="btn btn-primary btn-sm illegal-btn">查看</button></td>';
											} else {
												e+='<td class="text-center"'+ curRowspanStr +' >-</td>';
											}
										}
        		        				// 申诉状态
        		        				if ( _a.appeal ){
        		        					e+='<td class="text-center"'+ curRowspanStr +' >'+ MY_APPEAL.getAppealStTxt(_a.appeal.appealSt) +'</td>';//申述状态
                    						e+='<td class="text-center"'+ curRowspanStr +' >'+ (_a.appeal.appeal==null?'-':_a.appeal.appeal) +'</td>';// 申述内容
                    						
                    						// 申述图片
    		        						if (  _a.appeal && _a.appeal.appealAtta ){
    		        							e+='<td class="text-center"'+ curRowspanStr +' ><button type="button" name="showAppealAtta" class="btn btn-default btn-sm">查看</button></td>';
    		        						} else {
    		        							e+='<td class="text-center"'+ curRowspanStr +' >-</td>';
    		        						}
                    						
                    						e+='<td class="text-center"'+ curRowspanStr +' >'+ (_a.appeal.feedback==null?'-':_a.appeal.feedback) +'</td>';// 处理结果
                    						e+='<td class="text-center"'+ curRowspanStr +' >'+ (_a.appeal.supervisorName==null?'-':_a.appeal.supervisorName) +'</td>';// 处理人
        		        				} else {
        		        					e+='<td class="text-center"'+ curRowspanStr +' >-</td>';
        		        					e+='<td class="text-center"'+ curRowspanStr +' >-</td>';
        		        					e+='<td class="text-center"'+ curRowspanStr +' >-</td>';
        		        					e+='<td class="text-center"'+ curRowspanStr +' >-</td>';
											e+='<td class="text-center"'+ curRowspanStr +' >-</td>';
        		        				}
                						// 操作
                						e+='<td class="text-center" '+ curRowspanStr +' >-</td>';
            						} else {
            							// 种类
    		        					e+='<td class="text-center">'+ (_b.instrumentName == null?'-':_b.instrumentName)  + '</td>';
            							// 数量(箱)
    		        					if ( params.businessSubType == '101'){ //器械消毒时
    		        						e+='<td class="text-center">'+ (_b.quantity == null?'-':_b.quantity)  + '</td>';
    		        					}
            						}
            						e+='</tr>';
        						});
        					} else { //卫监手动更新为正常的数据
        						e+='<tr';
	        						e+= ' data-id="' + _a.id  + '"';
	        						if (  _a.appeal && _a.appeal.appealAtta ){ //申诉图片
	        							e+= ' data-appeal_atta="' + _a.appeal.appealAtta + '"';
	        						}
        						e+='>';
									e+='<td class="text-center">'+ _a.institutionShortName + '</td>';// 名称
			        				e+='<td class="text-center">'+ _a.serialDate + '</td>';// 日期
									if (_a.appeal) {
										if (_a.appeal.appealSt == 2 || _a.appeal.appealSt == 10) {
											e+='<td class="text-center">正常</td>';// 状态
										} else {
											e+='<td class="text-center"><font style="color:red;">异常</font></td>';// 状态
										}
									} else {
										e+='<td class="text-center">正常</td>';// 状态
									}
			        				e+='<td class="text-center message"  >'+(_a.message !=null && _a.msgId!= null?_a.message:'-')+'</td>';// 状态说明
			        				e+='<td class="text-center">-</td>';// 器械·场所名称
			        				if ( params.businessSubType == '101' ){ //器械消毒时
	    		        				e+='<td class="text-center">-</td>';// 数量
			        				}
			        				e+='<td class="text-center"  >-</td>';// 消毒方法
			        				// e+='<td class="text-center"  >-</td>';// 消毒时间
			        				e+='<td class="text-center"  >-</td>';// 消毒员
			        				if ( params.businessSubType == '101' ){ //器械消毒时
	    		        				// 试纸照片
	    		        				e+='<td class="text-center show_img"  >-</td>';
			        				}
			        				if ( _a.appeal ){
			        					e+='<td class="text-center" >'+ MY_APPEAL.getAppealStTxt(_a.appeal.appealSt) +'</td>';// 申诉状态
										e+='<td class="text-center" >'+ (_a.appeal.appeal==null?'-':_a.appeal.appeal) +'</td>';// 申述内容
										
										// 申述图片
		        						if (  _a.appeal && _a.appeal.appealAtta ){
		        							e+='<td class="text-center"><button type="button" name="showAppealAtta" class="btn btn-default btn-sm">查看</button></td>';
		        						} else {
		        							e+='<td class="text-center">-</td>';
		        						}
										
										e+='<td class="text-center" >'+ (_a.appeal.feedback==null?'-':_a.appeal.feedback) +'</td>';// 处理结果
										e+='<td class="text-center" >'+ (_a.appeal.supervisorName==null?'-':_a.appeal.supervisorName) +'</td>';// 处理人
			        				} else {
			        					e+='<td class="text-center" >-</td>';
			        					e+='<td class="text-center" >-</td>';
			        					e+='<td class="text-center" >-</td>';
			        					e+='<td class="text-center" >-</td>';
                                        e+='<td class="text-center" >-</td>';
			        				}
	        						// 操作
	        						e+='<td class="text-center"  >-</td>';
	    						e+='</tr>';
        					}
        					
        				} else {
        					e+='<tr ';
        					    e+= ' data-id="' + _a.id + '"';
	        					e+='  data-sel_row="sel_row'+ idx +'" ';
	        					e+= ' data-illegal_id="' + _a.id + '"';
	        					if (  _a.appeal && _a.appeal.appealAtta ){ //申诉图片
        							e+= ' data-appeal_atta="' + _a.appeal.appealAtta + '"';
        						}
        					e+='>';
								// 名称
								e+='<td class="text-center">'+ _a.institutionShortName + '</td>';
	        					// 日期
		        				e+='<td class="text-center">'+ _a.serialDate + '</td>';
	        					// 状态
		        				e+='<td class="text-center check_st"><font style="color:red;">异常</font></td>';
		        				// 状态说明
		        				e+='<td class="text-center message"><font style="color:red;">'+ _a.message + '</font></td>';
			        			// 来源
		        				e+='<td class="text-center">-</td>';
			        			// 种类
		        				e+='<td class="text-center">-</td>';
		        				if ( params.businessSubType == '101' ){ //器械消毒时
				        			// 数量(箱)
			        				e+='<td class="text-center">-</td>';
			        				// 消毒方法
			        				e+='<td class="text-center">-</td>';
		        				}
			        			// 消毒时间
		        				// e+='<td class="text-center">-</td>';
		        				// 消毒员
		        				e+='<td class="text-center">-</td>';
		        				// 申诉状态
		        				e+='<td class="text-center feedback_st">'+ MY_APPEAL.getAppealStTxt(_a.appeal.appealSt) +'</td>';
		        				// 申述内容
        						e+='<td class="text-center">'+ (_a.appeal.appeal==null?'-':_a.appeal.appeal) +'</td>';
        						// 申述图片
        						if (  _a.appeal && _a.appeal.appealAtta ){
        							e+='<td class="text-center"><button type="button" name="showAppealAtta" class="btn btn-default btn-sm">查看</button></td>';
        						} else {
        							e+='<td class="text-center">-</td>';
        						}
        						// 处理结果
        						e+='<td class="text-center feedback">'+ (_a.appeal.feedback==null?'-':_a.appeal.feedback) +'</td>';
        						// 处理人
        						e+='<td class="text-center supervisor_name">'+ (_a.appeal.supervisorName==null?'-':_a.appeal.supervisorName) +'</td>';
        						// 操作   // 0：  1：  -1： 2：
        						if (  _a.appeal.appealSt == 1 ){
        							e+='<td class="text-center"><button type="button" name="check" class="btn btn-default btn-sm">申诉处理</button></td>';
        						} else if ( _a.appeal.appealSt == 0 ) {
        							e+='<td class="text-center"><button type="button" name="closeIllegal" class="btn btn-default btn-sm">取消异常</button></td>';
        						} else {
        							e+='<td class="text-center">-</td>';
        						}
		        			e+='</tr>';
        				}
	        			
	        		});
	        		$tbodySpecimenDetailList.append(e);
	        		
	        	} else if (data.errcode == -1 ) {
	        		WeKnow.errorMsg( data.errMsg );
	        	} else {
	        		WeKnow.systemMsg('查询失败，请重新登录后再试');
	        	}
	        }
		});
	}
	
	/**
	 * 生成明细列表头部HTML文
	 * 
	 * @param type   101:器械 102：场所
	 * 
	 * */
	function getTblHead( type ){

		var e = '';
		
		if ( type == '101' ){
			e+='<tr >';
				e+='<th width="8%" class="text-center">机构名/科室</th>';
				e+='<th width="8%" class="text-center">检查日期</th>';
				e+='<th width="6%" class="text-center">状态</th>';
				e+='<th width="8%" class="text-center">状态说明</th>';
				e+='<th width="8%" class="text-center">器械名称</th>';
				e+='<th width="8%" class="text-center">数量</th>';
				e+='<th width="8%" class="text-center">消毒方法</th>';
				// e+='<th width="6%" class="text-center">消毒时间</th>';
				e+='<th width="6%" class="text-center">消毒员</th>';
				e+='<th width="6%" class="text-center">试纸照片</th>';
				e+='<th width="6%" class="text-center">申述状态</th>';
				e+='<th width="8%" class="text-center">申述内容</th>';
				e+='<th width="6%" class="text-center">照片</th>';
				e+='<th width="6%" class="text-center">处理意见</th>';
				e+='<th width="6%" class="text-center">处理人</th>';
				e+='<th width="6%" class="text-center">操作</th>';
			e+='</tr>';
		} else {
			e+='<tr >';
				e+='<th width="8%" class="text-center">机构名/科室</th>';
				e+='<th width="8%" class="text-center">检查日期</th>';
				e+='<th width="6%" class="text-center">状态</th>';
				e+='<th width="6%" class="text-center">状态说明</th>';
				e+='<th width="8%" class="text-center">消毒场地</th>';
				e+='<th width="8%" class="text-center">消毒方法</th>';
				// e+='<th width="6%" class="text-center">消毒时间</th>';
				e+='<th width="6%" class="text-center">消毒员</th>';
				e+='<th width="6%" class="text-center">申述状态</th>';
				e+='<th width="6%" class="text-center">申述内容</th>';
				e+='<th width="6%" class="text-center">照片</th>';
				e+='<th width="6%" class="text-center">处理意见</th>';
				e+='<th width="6%" class="text-center">处理人</th>';
				e+='<th width="8%" class="text-center">操作</th>';
			e+='</tr>';
		}
		
		return e;
	}
	
	// add by zhengxb 2019.6.21  end ---------------------------------------------------------
	return {
		loadDetailItem:loadDetailItem
	}
})();
